package com.scsj.supermarket.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.MarketBottomDialogBean;
import com.scsj.supermarket.bean.ModelDishBean;
import com.scsj.supermarket.bean.ModelShopCartBean;
import com.scsj.supermarket.interfaces.ShopCartInterface;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺购物车
 */

public class MarketBottomDialogAdapter extends RecyclerView.Adapter {

    private static String TAG = "PopupDishAdapter";
    private ModelShopCartBean mModelShopCart;
    private Context context;
    private int itemCount;
    private ArrayList<ModelDishBean> mModelDishList;
    private ShopCartInterface shopCartImp;

    public MarketBottomDialogAdapter(Context context, ModelShopCartBean modelShopCart) {
        this.mModelShopCart = modelShopCart;
        this.context = context;
        this.itemCount = modelShopCart.getDishAccount();
        this.mModelDishList = new ArrayList<>();
        mModelDishList.addAll(modelShopCart.getShoppingSingleMap().keySet());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_market_bottom_dialog_layout, parent, false);
        DishViewHolder viewHolder = new DishViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DishViewHolder dishholder = (DishViewHolder) holder;
        final ModelDishBean modelDish = getDishByPosition(position);
        if (modelDish != null) {
            dishholder.right_dish_name_tv.setText(modelDish.getDishName());
            dishholder.right_dish_price_tv.setText(modelDish.getDishPrice() + "");
            int num = mModelShopCart.getShoppingSingleMap().get(modelDish);

            dishholder.right_dish_add_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mModelShopCart.addShoppingSingle(modelDish)) {
                        notifyItemChanged(position);
                        if (shopCartImp != null)
                            shopCartImp.add(view, position);
                    }
                }
            });

            dishholder.right_dish_remove_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mModelShopCart.subShoppingSingle(modelDish)) {
                        mModelDishList.clear();
                        mModelDishList.addAll(mModelShopCart.getShoppingSingleMap().keySet());
                        itemCount = mModelShopCart.getDishAccount();
                        notifyDataSetChanged();
                        if (shopCartImp != null)
                            shopCartImp.remove(view, position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return this.itemCount;
    }

    public ModelDishBean getDishByPosition(int position) {
        return mModelDishList.get(position);
    }

    public ShopCartInterface getShopCartInterface() {
        return shopCartImp;
    }

    public void setShopCartInterface(ShopCartInterface shopCartImp) {
        this.shopCartImp = shopCartImp;
    }

    private class DishViewHolder extends RecyclerView.ViewHolder {
        private TextView right_dish_name_tv;
        private TextView right_dish_price_tv;
        private LinearLayout right_dish_layout;
        private ImageView right_dish_remove_iv;
        private ImageView right_dish_add_iv;

        public DishViewHolder(View itemView) {
            super(itemView);
            right_dish_name_tv = itemView.findViewById(R.id.right_dish_name);
            right_dish_price_tv = itemView.findViewById(R.id.right_dish_price);
            right_dish_layout = itemView.findViewById(R.id.right_dish_item);
            right_dish_add_iv = itemView.findViewById(R.id.right_dish_add_iv);
            right_dish_remove_iv = itemView.findViewById(R.id.right_dish_remove);
        }
    }
}
